﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace WebMVC.Controllers
{
    public class HangHoaController : Controller
    {
        databaseDataContext db = new databaseDataContext();
        //
        // GET: /HangHoa/
        [Authorize(Roles = "0")]
        public ActionResult Index()
        {
            ViewData["Items"] = from h in db.HangHoas
                                select h;
            return View();
        }

        //
        // GET: /HangHoa/Details/5
        [Authorize(Roles = "0")]
        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /HangHoa/Create
        [Authorize(Roles = "0")]
        public ActionResult Create()
        {
            IEnumerable<SelectListItem> listNhomHang = db.NhomHangs.Select(c => new SelectListItem
            {
                Value = c.ID.ToString(),
                Text = c.Ten.ToString()
            });

            IEnumerable<SelectListItem> listDVQD = db.DonViQuiDois.Select(c => new SelectListItem
            {
                Value = c.ID.ToString(),
                Text = c.Don_vi_goc.ToString()
            });

            ViewData["ListNhomHang"] = listNhomHang;
            ViewData["ListDVQD"] = listDVQD;
            return View();
        } 

        //
        // POST: /HangHoa/Create
        [Authorize(Roles = "0")]
        [HttpPost]
        public ActionResult Create(HangHoa hanghoa)
        {
            try
            {
                // TODO: Add insert logic here
                hanghoa.Ngay_cap_nhat_cuoi = DateTime.Now;
                db.HangHoas.InsertOnSubmit(hanghoa);
                db.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                IEnumerable<SelectListItem> listNhomHang = db.NhomHangs.Select(c => new SelectListItem
                {
                    Value = c.ID.ToString(),
                    Text = c.Ten.ToString()
                });

                IEnumerable<SelectListItem> listDVQD = db.DonViQuiDois.Select(c => new SelectListItem
                {
                    Value = c.ID.ToString(),
                    Text = c.Don_vi_goc.ToString()
                });

                ViewData["ListNhomHang"] = listNhomHang;
                ViewData["ListDVQD"] = listDVQD;
                return View();
            }
        }
        
        //
        // GET: /HangHoa/Edit/5
        [Authorize(Roles = "0")]
        public ActionResult Edit(int id)
        {
            HangHoa model = (from m in db.HangHoas
                                           where m.ID == id
                                           select m).SingleOrDefault();
            IEnumerable<SelectListItem> listNhomHang = db.NhomHangs.Select(c => new SelectListItem
            {
                Value = c.ID.ToString(),
                Text = c.Ten.ToString()
            });

            IEnumerable<SelectListItem> listDVQD = db.DonViQuiDois.Select(c => new SelectListItem
            {
                Value = c.ID.ToString(),
                Text = c.Don_vi_goc.ToString()
            });

            ViewData["ListNhomHang"] = listNhomHang;
            ViewData["ListDVQD"] = listDVQD;
            return View(model);
        }

        //
        // POST: /HangHoa/Edit/5
        [Authorize(Roles = "0")]
        [HttpPost]
        public ActionResult Edit(int id, HangHoa hanghoa)
        {
            HangHoa model = (from m in db.HangHoas
                             where m.ID == id
                             select m).SingleOrDefault();
            try
            {
                // TODO: Add update logic here
                model.Ten = hanghoa.Ten;
                model.Ma_hang = hanghoa.Ma_hang;
                model.Thue = hanghoa.Thue;
                model.Chiet_khau = hanghoa.Chiet_khau;
                model.Gia_nhap = hanghoa.Gia_nhap;
                model.Gia_ban_le = hanghoa.Gia_ban_le;
                model.Gia_ban_si = hanghoa.Gia_ban_si;
                model.Ma_nhom_hang = hanghoa.Ma_nhom_hang;
                model.Ghi_chu = hanghoa.Ghi_chu;
                model.Ma_don_vi_qui_doi = hanghoa.Ma_don_vi_qui_doi;
                model.Ngay_cap_nhat_cuoi = DateTime.Now;
                db.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                IEnumerable<SelectListItem> listNhomHang = db.NhomHangs.Select(c => new SelectListItem
                {
                    Value = c.ID.ToString(),
                    Text = c.Ten.ToString()
                });

                IEnumerable<SelectListItem> listDVQD = db.DonViQuiDois.Select(c => new SelectListItem
                {
                    Value = c.ID.ToString(),
                    Text = c.Don_vi_goc.ToString()
                });

                ViewData["ListNhomHang"] = listNhomHang;
                ViewData["ListDVQD"] = listDVQD;
                return View(model);
            }
        }

        //
        // GET: /HangHoa/Delete/5
        [Authorize(Roles = "0")]
        public ActionResult Delete(int id)
        {
            var model = (from h in db.HangHoas
                         where h.ID == id
                         select h).Take(1).SingleOrDefault();
            db.HangHoas.DeleteOnSubmit(model);
            db.SubmitChanges();
            return RedirectToAction("Index");
        }

        //
        // POST: /HangHoa/Delete/5
        [Authorize(Roles = "0")]
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
